                 
#include "mudport.h"

////////////////////////////////////////////////////////////////////////////////
opcd MudPort::read( net_out dynarray<charstr>& str )
{
    str.takeover(_out_buffer);
    return 0;
}

////////////////////////////////////////////////////////////////////////////////
opcd MudPort::send( net_in const charstr& str )
{
	charstr tmp, tmp2;

	switch(_state)
	{
		case 2:
			_in_buffer.push(str);
			break;
		case 0:
			//_out_buffer.push(charstr("Your name is ") + str);
			tmp = str;
			tmp.trim();
			_cmdin->set_name(tmp);
			tmp.reset();
			tmp << "password: " << COL_BACK_BLACK << COL_BLACK;
			_out_buffer.push(tmp);
			_state = 1;
			break;
		case 1:
			{
				tmp2 = str;
				tmp2.trim(true, false);
				if (tmp2 != "")
				{
					tmp.reset();
					tmp << "password: " << COL_BACK_BLACK << COL_BLACK;
					_out_buffer.push(tmp);
					_state = 1;
					break;
				}/* else {
				// disconnect
				//_state = 0;
				return 0;
				}*/				
				void *cr;
				_cmdin->get_name(tmp);
				_server.get_creature(tmp, &cr);
				_creature = cr;
				if (cr == NULL) {
					_out_buffer.push("No such creature! Trying to create new.....not supported now\r\n");
					_out_buffer.push("Enter your name: ");
					_state = 0;
					break;
				}
				_out_buffer.push(charstr("\r\nYou are logged as '") + tmp + "' pass('" + tmp2 + "')\r\n\r\n");
				_cmdin->set_option( cmd_interface::OPTION_ALLOW_SHARED_ACCESS, 1 );
				_server.register_client( _cmdin->get_id() );	
				_state = 2;
			}
			break;
	}
	flush();
	return 0;
}
